package com.pzh.guli.service.base.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.HashMap;
import java.util.Map;

// todo @Data
@Data
//@Getter
//@Setter
@ToString
@ApiModel(value = "统一返回结果")
public class Result {
    @ApiModelProperty(value = "操作是否成功")
    private boolean success;
    @ApiModelProperty(value = "状态码")
    private Integer code;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private Map<String,Object> data=new HashMap<>();
    //成功
    public static Result ok(){
        Result result=new Result();
        result.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
        result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        return result;
    }
    //操作失败
    public static Result error(){
        Result result=new Result();
        result.setSuccess(ResultCodeEnum.UNKNOWN_REASON.getSuccess());
        result.setMessage(ResultCodeEnum.UNKNOWN_REASON.getMessage());
        result.setCode(ResultCodeEnum.UNKNOWN_REASON.getCode());
        return result;
    }
    public static Result setResult(ResultCodeEnum codeEnum){
        Result result=new Result();
        result.setSuccess(codeEnum.getSuccess());
        result.setCode(codeEnum.getCode());
        result.setMessage(codeEnum.getMessage());
        return result;
    }
    public Result data(String key,Object value){
        this.data.put(key,value);
        return this;
    }
    public Result success(Boolean success){
        this.success=success;
        return this;
    }
    public Result code(Integer code){
        this.code=code;
        return this;
    }
    public Result message(String message){
        this.message=message;
        return this;
    }
}
